package a09_贪心算法;

/**
 * <p>
 * a11_加油站复习1
 * </p>
 *
 * @author flyduck
 * @since 2025/1/14
 */
public class a11_加油站复习1 {
    public int canCompleteCircuit(int[] gas, int[] cost) {
        int currentSum = 0;
        int totalSum = 0;//总和
        int startIdx = 0;
        for (int i = 0; i < gas.length; i++) {
            currentSum = currentSum + (gas[i] - cost[i]);
            totalSum = totalSum + (gas[i] - cost[i]);
            if(currentSum < 0){
                startIdx = i + 1;
                currentSum = 0;
            }
        }
        if(totalSum >= 0){
            return startIdx;
        }else {
            return -1;
        }
    }
}
